package com.hsnn.medstgmini.crypto;

/**
 * @category 编码，解码
 * @author 单大伟
 * @date 2015-08-30
 */
public class CryptoUtil {
	
	/**
	 * @category 编码
	 * @author 单大伟
	 * @date 2015-08-30
	 * @param 要编码的字节
	 * @return String 编码结果
	 */
	public static String encode(byte[] orig) {
        StringBuffer result = new StringBuffer();
        String hx;

        for( int i = 0; i < orig.length; i++) {
            hx = Integer.toString(0xff & orig[i],16);
		    if (hx.length() == 1) {
		    	hx = "0" + hx;
		    }
            result.append(hx);
        }
        return result.toString();
    }
    
	/**
	 * @category 解码
	 * @author 单大伟
	 * @date 2015-08-30
	 * @param 要解码的字节
	 * @return String 解码结果
	 */
	public static byte[] decode(String orig) {
	 	byte[] str = orig.getBytes();
        byte[] b = new byte[2];
        int origLen = (int)(str.length / 2);
        byte[] result = new byte[origLen];

        for(int i = 0; i < origLen; i++) {
            b[0] = str[i * 2];
            b[1] = str[i * 2 + 1];
            String strVal = new String(b);
            char intVal = (char)Integer.parseInt(strVal, 16);
            result[i] = (byte)intVal;
        }
        return result;
    }
}